package com.fqh.controller;

import com.fqh.entity.JsonResult;
import com.fqh.entity.Login;
import com.fqh.entity.Order1;
import com.fqh.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Scope(value = "prototype")
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("orderFood.do")
    public String orderFood(HttpServletRequest request, String id, String number) {
        HttpSession session = request.getSession();
        Login user = (Login) session.getAttribute("user");
        orderService.orderFood(user.getLoginName(), id, number);
        return "ok";
    }

    @PostMapping("addMenu.do")
    public JsonResult addMenu(@RequestParam("foodId") String foodId, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Login user = (Login) session.getAttribute("user");
        try {
            orderService.addMenu(user.getLoginName(), foodId);
            return new JsonResult("加入购物车成功", true);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("加入购物车失败", false);
        }
    }

    @PostMapping("findAlreadyOrder.do")
    public Integer alreadyOrder(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Login user = (Login) session.getAttribute("user");
        Integer count = orderService.alreadyOrder(user.getLoginName());
        return count;
    }

    @PostMapping("findAllList.do")
    public List findAllList(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Login user = (Login) session.getAttribute("user");
        List list = orderService.findAllList(user.getLoginName());
        return list;
    }

    @PostMapping("addNumber.do")
    public String addNumber(@RequestParam("number") String foodId, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Login user = (Login) session.getAttribute("user");
        try {
            orderService.addNumber(foodId, user.getLoginName());
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }

    @PostMapping("reduceNumber.do")
    public String reduceNumber(@RequestParam("number") String foodId, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Login user = (Login) session.getAttribute("user");
        String str = orderService.reduceNumber(foodId, user.getLoginName());
        return str;
    }

    @PostMapping("delFoodById.do")
    public String delFoodById(String foodId, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Login user = (Login) session.getAttribute("user");
        String str = orderService.delFoodById(foodId, user.getLoginName());
        return str;
    }

    @PostMapping("findAllOrder.do")
    public List<Order1> findAllOrder(HttpServletRequest request) {
        Login user = (Login) request.getSession().getAttribute("user");
        List<Order1> order = orderService.findAllOrder(user.getLoginId());
        return order;
    }
}
